﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Frm_Confirm : Form
    {
        private Form1 _form1;
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement xmlelem;
        public Frm_Confirm( Form1 form1)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            _form1 = form1;
        }

        private void btn_Comfirm_Click(object sender, EventArgs e)
        {
            if (!(textBox1.Text == ""))
            {
                string name = "Root" + DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.ToString("HHmmss");
                xmlDoc.Load("TreeXml.xml");
                XmlNode root = xmlDoc.SelectSingleNode("Tree");
                xmlelem = xmlDoc.CreateElement("Root");
                xmlelem.SetAttribute("Name", name);
                xmlelem.SetAttribute("Text", textBox1.Text);
                if (textBox1.Text == null)    //调试专用
                {
                    MessageBox.Show("P3在这");
                }
                xmlelem.SetAttribute("IP", "127.0.0.1");
                root.AppendChild(xmlelem);
                xmlDoc.Save("TreeXml.xml");
                _form1.Trv_Config.Nodes.Add(name, textBox1.Text, 3, 3);
                _form1.toolStripStatusLabel1.Text = "新建根节点成功";
                _form1.Trv_Config.SelectedNode = _form1.Trv_Config.Nodes[name];
                textBox1.Clear();
                btn_Comfirm.Visible = false;
                btn_Cancel.Visible = false;
                comboBox1.Visible = true;
                label2.Visible = true;
                this.TopMost = false;
                this.Visible = false;
            }
            else
            {
                MessageBox.Show("请输入环境名称！");
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            btn_Comfirm.Visible = false;
            btn_Cancel.Visible = false;
            comboBox1.Visible = true;
            label2.Visible = true;
            textBox1.Clear();
            this.TopMost = false;
            this.Visible = false;
        }

        private void btn_Confirm1_Click(object sender, EventArgs e)
        {
            if (!(textBox1.Text == ""))
            {
                string[] dt = _form1.GetDate();
                //TreeNode CurrentNode = _form1.Trv_Config.SelectedNode;
                TreeNode CurrentNode = _form1.Trv_Config.Nodes[textBox2.Text];
                string name = "Node" + DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.ToString("HHmmss");
                xmlDoc.Load("TreeXml.xml");
                XmlNode xd = xmlDoc.SelectSingleNode("//*[@Name='" + CurrentNode.Name + "']");
                XmlElement xe = (XmlElement)xd;
                xmlelem = xmlDoc.CreateElement("Node");
                xmlelem.SetAttribute("Name", name);
                xmlelem.SetAttribute("Text", textBox1.Text);
                if (textBox1.Text == null)    //调试专用
                {
                    MessageBox.Show("P4在这");
                }
                xmlelem.SetAttribute("Mulu",dt[0]);
                xmlelem.SetAttribute("IP", dt[1]);
                xmlelem.SetAttribute("DbName", dt[2]);
                xmlelem.SetAttribute("Account", dt[3]);
                xmlelem.SetAttribute("Password", dt[4]);
                xmlelem.SetAttribute("BootType", dt[5]);
                xmlelem.SetAttribute("IP1", dt[6]);
                xmlelem.SetAttribute("DbName1", dt[7]);
                xmlelem.SetAttribute("Account1", dt[8]);
                xmlelem.SetAttribute("Password1", dt[9]);
                xmlelem.SetAttribute("BootType1", dt[10]);
                xmlelem.SetAttribute("IP2", dt[11]);
                xmlelem.SetAttribute("DbName2", dt[12]);
                xmlelem.SetAttribute("Account2", dt[13]);
                xmlelem.SetAttribute("Password2", dt[14]);
                xmlelem.SetAttribute("BootType2", dt[15]);
                xmlelem.SetAttribute("Version", _form1.comboBoxproduct2.Text);
                xmlelem.SetAttribute("E_Info", _form1.comboBoxConfig.Text);
                xe.AppendChild(xmlelem);
                xmlDoc.Save("TreeXml.xml");
                CurrentNode.Nodes.Add(name,textBox1.Text,8,4);
                _form1.toolStripStatusLabel1.Text = "新建子节点成功";
                CurrentNode.ExpandAll();
                _form1.Trv_Config.SelectedNode = CurrentNode.Nodes[name];
                btn_Confirm1.Visible = false;
                btn_Cancel1.Visible = false;
                textBox1.Clear();
                this.TopMost = false;
                this.Visible = false;
                _form1.Listtree.Clear();
            }
            else
            {
                MessageBox.Show("请输入环境名称！");
            }
        }

        private void btn_Cancel1_Click(object sender, EventArgs e)
        {
            btn_Confirm1.Visible = false;
            btn_Cancel1.Visible = false;
            textBox1.Clear();
            this.TopMost = false;
            this.Visible = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox2.Text = _form1.Listtree[comboBox1.SelectedIndex].ToString();
        }
    }
}
